/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.startup;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.httpservice.HttpGetter;
import cn.ac.iie.di.wx.utils.DateUtil;
import cn.ac.iie.di.wx.utils.RedisUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 * 用于补全信息
 *
 * @author Administrator
 */
public class CplStart {
    
    static Logger logger = Logger.getLogger(CplStart.class.getName());
    
    public static void main(String[] args) {
        try {
            init();
            HttpGetter httpGetter = new HttpGetter();
            
            String redisHosts = ((String) RuntimeEnv.getParam(RuntimeEnv.REDIS_HOSTS));
            String masterName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_MASTER_NAME);
            String dbName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_DB_NAME);
            
            String user = (String) RuntimeEnv.getParam(RuntimeEnv.USER);
            String password = (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD);
            String yesterdayDate = DateUtil.getYesterdayDate();
            
            RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
            
            logger.info("date:" + (String) RuntimeEnv.getParam(RuntimeEnv.DATE)+"\t"+DateUtil.getTheDayBeforeDate((String) RuntimeEnv.getParam(RuntimeEnv.DATE)));
            String theDayBeforeDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE)
                    + DateUtil.getTheDayBeforeDate((String) RuntimeEnv.getParam(RuntimeEnv.DATE)));
//            logger.info(theDayBeforeDataSize);
            String yesterdayDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE)
                    + (String) RuntimeEnv.getParam(RuntimeEnv.DATE));
            String allCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.ALLCOUNT)
                    + (String) RuntimeEnv.getParam(RuntimeEnv.DATE));
            String successCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SUCCESSCOUNT)
                    + (String) RuntimeEnv.getParam(RuntimeEnv.DATE));
            logger.info(yesterdayDataSize+"\t"+theDayBeforeDataSize+"\t"+allCount+"\t"+successCount);
            //日期DATE为，今天发送的数据，填昨天的日期
            reWriteByProperties(
                    (String) RuntimeEnv.getParam(RuntimeEnv.yesterdayDataSize),
                    (String) RuntimeEnv.getParam(RuntimeEnv.theDayBeforeDataSize),
                    (String) RuntimeEnv.getParam(RuntimeEnv.allCount),
                    (String) RuntimeEnv.getParam(RuntimeEnv.successCount),
                    (String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE),
                    (String) RuntimeEnv.getParam(RuntimeEnv.DATE),
                    (String) RuntimeEnv.getParam(RuntimeEnv.USER),
                    (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD), httpGetter);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(CplStart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static List<Date> listDate(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        //lDate.add(dBegin);  
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间    
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间    
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后    
        while (dEnd.after(calBegin.getTime())) {
            lDate.add(calBegin.getTime());
            calBegin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return lDate;
    }
    /*
     根据日期进行数据覆盖
     */
    
    public static void reWrite(RedisUtil msgRedisUtil, String yesterdayDate, String user, String password, HttpGetter httpGetter) {
        String massAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSALL) + yesterdayDate);//模拟
        String massSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.MASSSUC) + yesterdayDate);
        String singleAll = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLEALL) + yesterdayDate);
        String singleSuc = msgRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SINGLESUC) + yesterdayDate);
//        logger.info("massAll=" + massAll + "massSuc=" + massSuc + "singleAll=" + singleAll + "singleSuc=" + singleSuc);

        // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
//            String evaluate = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE)+yesterdayDate);
        if (massAll != null && massSuc != null && singleAll != null && singleSuc != null) {
            String msgrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                    + "&massAll=" + massAll + "&massSuc=" + massSuc + "&singleAll=" + singleAll + "&singleSuc=" + singleSuc;
            String msg_url = (String) RuntimeEnv.getParam(RuntimeEnv.MSG_URL) + "?";
            logger.info("request string :"+msg_url + msgrequest);
            httpGetter.doGet(msg_url + msgrequest);
        } else {
            logger.warn("can not get msg datas from redis");
        }
    }
    
    public static void reWriteByProperties(String yesterdayDataSize, String theDayBeforeDataSize, String allCount,
            String successCount, String evaluate, String yesterdayDate, String user, String password, HttpGetter httpGetter) {
        logger.info("yesterdayDataSize=" + yesterdayDataSize + "theDayBeforeDataSize=" + theDayBeforeDataSize + "allCount=" + allCount + "singleSuc=" + successCount);

        // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
//            String evaluate = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.EVALUATE)+yesterdayDate);
        if (yesterdayDataSize != null && allCount != null && successCount != null && theDayBeforeDataSize != null) {
            
            logger.info("yesterdayDataSize=" + yesterdayDataSize + "theDayBeforeDataSize=" + theDayBeforeDataSize + "allCount=" + allCount + "successCount=" + successCount + "evaluate=" + evaluate);
            String userrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                    + "&dataSize=" + yesterdayDataSize + "&allCount=" + allCount + "&successCount=" + successCount + "&evaluate=" + evaluate;
            String user_url = (String) RuntimeEnv.getParam(RuntimeEnv.USER_URL) + "?";
            
            httpGetter.doGetByDate(user_url + userrequest, yesterdayDate);
        } else {
            logger.warn("can not get user datas from redis");
        }
    }
    
    public static void init() throws Exception {
        String configurationFileName = "serverport.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            throw new Exception("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
        } catch (Exception ex) {
            throw new Exception("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }
}
